﻿
// <copyright file="RunHelper.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The run helper.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.ServiceProcess
{
    using System.Configuration;
    using System.ServiceProcess;
    using System.Threading;
    using System.Windows.Forms;

    using SubhadraSolutions.Sharp.Utils.Contracts;

    /// <summary>
    /// The run helper.
    /// </summary>
    public static class RunHelper
    {
        #region Constants

        /// <summary>
        /// The confi g_ sectio n_ name.
        /// </summary>
        public const string CONFIG_SECTION_NAME = "controllables";

        #endregion Constants

        #region Static Fields

        /// <summary>
        /// The section.
        /// </summary>
        private static ControllablesConfigurationSection _section;

        #endregion Static Fields

        #region Public Methods and Operators

        /// <summary>
        /// The run.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool Run()
        {
            _section = (ControllablesConfigurationSection)ConfigurationManager.GetSection(CONFIG_SECTION_NAME);
            bool runAsService = false;
            string runAsServiceString = ConfigurationManager.AppSettings["runAsService"];
            if (runAsServiceString != null)
            {
                bool.TryParse(runAsServiceString, out runAsService);
            }

            if (runAsService)
            {
                var adapter = new ControllableAsServiceBaseAdapter { Controllables = _section.Controllables, ServiceName = ConfigurationManager.AppSettings["ServiceName"] };
                adapter.Initialize();
                var servicesToRun = new ServiceBase[] { adapter };
                ServiceBase.Run(servicesToRun);
            }
            else
            {
                GeneralHelper.InitializeInitializables(_section.Controllables);
                foreach (IControllable controllable in _section.Controllables)
                {
                    controllable.Controller.Start();
                }

                var winformThread = new Thread(openForm);
                winformThread.SetApartmentState(ApartmentState.STA);
                winformThread.IsBackground = false;
                winformThread.Start();
            }

            return runAsService;
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// The open form.
        /// </summary>
        private static void openForm()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MetricsForm(_section.Controllables));
        }

        #endregion Methods

        // public static bool Run()
        // {
        // _section = (ControllablesConfigurationSection)ConfigurationManager.GetSection(CONFIG_SECTION_NAME);
        // bool runAsService = false;
        // string runAsServiceString = ConfigurationManager.AppSettings["runAsService"];
        // if (runAsServiceString != null)
        // {
        // bool.TryParse(runAsServiceString, out runAsService);
        // }
        // if (runAsService)
        // {
        // var adapter = new ControllableAsServiceBaseAdapter();
        // adapter.Controllables = _section.Controllables;
        // adapter.ServiceName = ConfigurationManager.AppSettings["ServiceName"];
        // adapter.Initialize();
        // var servicesToRun = new ServiceBase[] { adapter };
        // ServiceBase.Run(servicesToRun);
        // }
        // else
        // {
        // GeneralHelper.InitializeInitializables(_section.Controllables);
        // foreach (var controllable in _section.Controllables)
        // {
        // controllable.Controller.Start();
        // }
        // Application.EnableVisualStyles();
        // Application.SetCompatibleTextRenderingDefault(false);
        // Application.Run(new MetricsForm(_section.Controllables));
        // }
        // return runAsService;
        // }
    }
}